<?php

class NewsController extends BaseController {
	
	public function showNewsDetails($lang, $newsID, $title, $status ){
				
		$newsDetails = News::status(Options::STATUS_ACTIVE)->language()->newsID($newsID)->joinUsers()
			->get(array(Options::DB_TABLE_NEWS.'.id',Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.content', Options::DB_TABLE_NEWS.'.noOfComments', 
						Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_NEWS.'.featurePhotoDesc', 
						Options::DB_TABLE_NEWS.'.created_at', Options::DB_TABLE_USERS.'.username', Options::DB_TABLE_CATEGORIES.'.name' ));
		
		$headerMenu = !empty($newsDetails[0]) ? $newsDetails[0]->name : "" ;
			 		
		$catList = Category::status(Options::STATUS_ACTIVE)->language()->orderBySequence("asc")->get(array(Options::DB_TABLE_CATEGORIES.'.id',Options::DB_TABLE_CATEGORIES.'.name'));
		
		$previousPost = News::status(Options::STATUS_ACTIVE)->language()->categoryName($headerMenu)->take(1)->recordBefore($newsID)->orderByCreated("desc")
			->get(array(Options::DB_TABLE_NEWS.'.id',Options::DB_TABLE_NEWS.'.title'));
		
		$nextPost = News::status(Options::STATUS_ACTIVE)->language()->categoryName($headerMenu)->take(1)->recordAfter($newsID)
			->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title'));
		
		//get list of comments of this news		
		$comments = Comment::newsID($newsID)->isPrimary($newsID)->get(array(Options::DB_TABLE_COMMENTS.'.commenter',Options::DB_TABLE_COMMENTS.'.comment' ));
		
		//dd(DB::getQueryLog());
		return View::make( 'newsDetails', array( "headerMenu" => $headerMenu, "catList" => $catList, "previousPost" => $previousPost, 
							"nextPost" => $nextPost, "newsDetails" => $newsDetails, "comments" => $comments, "status" => $status));
	}
	
	//admin
	
	public function showNews(){
		$emptyNews = new News(); 
		$name = Input::get('name');
		if(!empty($name) && $name !=""){
			 $emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.title','LIKE','%'.$name.'%');
		}
		
		$category = Input::get('category');		
		if(!empty($category)&& $category!="All" ){
			
			$emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.categoryID','=',$category);
		}
		
		$daterange = Input::get('daterange');		
		if(!empty($daterange) && $daterange!=""){
			$str = explode('-', $daterange);
			if(count($str)>1){
				$start_time = date('Y-m-d 00:00:00',strtotime($str[0]));
				$end_time = date('Y-m-d 23:59:59',strtotime($str[1]));	
			}else if(count($str)==1){
				$start_time = date('Y-m-d 00:00:00',strtotime($str[0]));
				$end_time = date('Y-m-d 23:59:59',strtotime($str[0]));	
			}else{
				$start_time = date('Y-m-d 00:00:00');
				$end_time = date('Y-m-d 23:59:59');	
			}					
			//format 
			$emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.created_at','>=',$start_time);
			$emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.created_at','<=',$end_time);
		}
		$st = Input::get('st');
		if(!empty($st)&& $st!="All"){
			$emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.status','=',$st);
		}
		$emptyNews = $emptyNews->where(Options::DB_TABLE_NEWS.'.status','<>','-1');
		$emptyNews = $emptyNews->join(Options::DB_TABLE_CATEGORIES, Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID')
                   ->select(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.categoryID', Options::DB_TABLE_NEWS.'.status', Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_NEWS.'.created_at', Options::DB_TABLE_CATEGORIES.'.name')
                ;
		
		$emptyNews->distinct();
		$data = $emptyNews->paginate(Config::get('config.number_page')); //->paginate(Config::get('config.number_page'));
		
		//$data = new News();
		$categories = new Category();	
		return View::make( 'admin.news.index')
		->with('dataAll',$data)
		->with('listCategory',$categories->getListCategory())
		->with('statusAll',Config::get('config.category_status'))
		->with('name',$name)
		->with('st',$st)
		->with('category',$category)
		->with('daterange',$daterange)
		->with('title',"News Sabahkini");
	}
	
	public function getEdit($id){	
		$dataEdit = News::find(intval($id));
		$check = true;
		if($dataEdit){
			if($dataEdit->status!="-1"){
				$data = new Category();
				return View::make('admin.news.edit')->with('dataEdit',$dataEdit)
				   ->with('listCategory',$data->getListCategory())
			       ->with('allStatus',Config::get('config.news_status'))
			       ->with('allLang',Language::get())
			       ->with('title','Edit News Sabahkini');
			}else{
				$check=false;			
			}     
		}else{
				$check = false;
			
		}
		if($check==false){
			return Redirect::to(Config::get('config.domain').'news')
				->withInput()->with('errMg', 'Error, Please Try Again !'); 
		}
       
	}
	public function update($id){
		    $news = News::find(intval($id)); 		
	        $news->categoryID = Input::get('categoryID');		
			$news->title=Input::get('title');
			$news->featurePhoto=Input::get('featurePhoto');
			$news->descriptions=Input::get('descriptions');
			$news->content=Input::get('content');
			$news->status=Input::get('status'); 
	 		 if($news->addNew()){
				return Redirect::to(Config::get('config.domain').'news')
					->withInput()->with('success', 'News Edited Successfully !'); 					
			}else{
				return Redirect::to(Config::get('config.domain').'news/edit/'.$id); 
			}    
	
	}
	

	
	public function postCreate(){
		$news  = new News();		
		$news->categoryID = Input::get('categoryID');		
		$news->title=Input::get('title');
		$news->featurePhoto=Input::get('featurePhoto');
		$news->descriptions=Input::get('descriptions');
		$news->content=Input::get('content');
		$news->status=Input::get('status');		
		if($news->addNew()){
				return Redirect::to(Config::get('config.domain').'news')
					->withInput()->with('success', 'News Created Successfully !'); 					
			}else{
				return Redirect::to(Config::get('config.domain').'news/add')
					->withInput()->with('errMg', 'Error, Please Try Again !'); 
		}
	}
	public function delete($id){	
	    $news = News::find(intval($id)); 
	    $check = false;
	    if($news){
		    $news->status=-1;
	       	if($news->addNew()){
	       		$check = true;	
			}
	    }
		if($check==true){
			return Redirect::back()
				->withInput()->with('success', 'News Deleted Successfully !'); 
		}else{
			return Redirect::back()
				->withInput()->with('errMg', 'Error, Please Try Again !');
		}
	}
	public function changes($id){		
		$str = explode('-', $id);
		$check = false;
		if(count($str)==2){
			if(intval($str[0])&& in_array($str[1],Config::get('config.category_status'))){
				$news = News::find(intval($str[0])); 
				foreach(Config::get('config.category_status') as $key=>$value){
						if($value==$str[1]){
							 $news->status = $key;
							 break;
						}
				}
				if($news->addNew()){
					$check = true;
				}  
			}
		}
		if($check==true){
			return Redirect::back()
				->withInput()->with('success', 'Status Changes Successfully !'); 
		}else{
			return Redirect::back()
				->withInput()->with('errMg', 'Error, Please Try Again !');
		}
		
	}
	
}